//
//  XMLLoader
//
//  Created by Wonil Suh on 2009-06-20.
//  Copyright (c) 2009 secret toilet. All rights reserved.
//  Contact: wosuh@secretToilet.com
//  
//  DESCRIPTION
//  

package com.wonilsuh.xml {
	
	// import
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.events.IOErrorEvent;
	import flash.events.HTTPStatusEvent;
	
	public class XMLLoader extends Object implements IEventDispatcher {

		// properties
		public static var _self:XMLLoader;
		
		public var xmlData:XML;
		private var urlLoader:URLLoader;
		
		private var dispatcher:EventDispatcher;
		
		// timeline elements
		
		// constants
		public static const XML_LOAD_COMPLETE:String = "XML_LOAD_COMPLETE";
		
		//----------------------------------------
		// constructor
		public function XMLLoader() {
			//trace("XMLLoader has been constructed.");
			
			_self = this;
			
			dispatcher = new EventDispatcher(this);
		}

		//----------------------------------------
		// init methods
		public function init(xmlUrl:String):void {
			trace("XMLLoader.init() at "+xmlUrl);
			
			urlLoader = new URLLoader(new URLRequest(xmlUrl));
			
			urlLoader.addEventListener(Event.COMPLETE, onXMLLoadComplete);
			urlLoader.addEventListener(ProgressEvent.PROGRESS, onXMLLoadProgress);
			urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onXMLLoadError);
			urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onXMLHTTPStatusEvent);
			
		}
		
		//----------------------------------------
		// methods
		private function onXMLLoadProgress(e:ProgressEvent):void {
			trace("XMLLoader.onXMLLoadProgress()...");
			dispatchEvent(e.clone());
		}
		
		private function onXMLLoadComplete(e:Event):void {
			trace("XMLLoader.onXMLLoadComplete()...");
			xmlData = new XML(e.target.data);
			//trace(xmlData+", length is "+xmlData.school.length());
			
			dispatchEvent(new Event(XML_LOAD_COMPLETE));
		}
		
		protected function onXMLLoadError(pEvent:IOErrorEvent):void {
			trace("XMLLoader.onXMLLoadError()...");
			
		}
		
		protected function onXMLHTTPStatusEvent(pEvent:HTTPStatusEvent):void {
			trace("XMLLoader.onXMLHTTPStatusEvent()...");
			
		}
		
		//----------------------------------------
		// getters and setters
		public function getXML():XML {
			//trace(".getXML()...");
			return xmlData;
		}
		
		public static function getInstance():XMLLoader {
			//trace(".getInstance()...");
			return _self;
		}
		
		//----------------------------------------
		// interface implementations
		
		//----------------------------------------
		// interface implementations: IEventDispatcher
		public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
			dispatcher.addEventListener(type, listener, useCapture, priority);
		}

		public function dispatchEvent(evt:Event):Boolean{
			return dispatcher.dispatchEvent(evt);
		}

		public function hasEventListener(type:String):Boolean{
			return dispatcher.hasEventListener(type);
		}

		public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void{
			dispatcher.removeEventListener(type, listener, useCapture);
		}

		public function willTrigger(type:String):Boolean {
			return dispatcher.willTrigger(type);
		}
		
	}
}